package com.tsll.utill;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;


/**
 * @author: Tian
 * @version: 1.0
 * @since: 1.0
 * @date: 2024/3/4/16:43
 */
public class OssUtils {

    public static String uploadOss(MultipartFile myfile){
        // 自己的endpoint
        String endpoint = "oss-cn-qingdao.aliyuncs.com";
        //bucketNname的名称
        String bucketName = "mobile-library";
        // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String filename = UUID.randomUUID().toString().replace("-","")+myfile.getOriginalFilename();
        String objectName = filename; //上传到oss的名称

        String accessKeyId="LTAI5t9Ce9bLAWk69ZwrMSqX";
        String secretAccessKey="9FmTIp4G7KTvykMyapg8aPMaq3gZDg";
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,secretAccessKey);
        try {
            ossClient.putObject(bucketName, objectName, myfile.getInputStream());
            return "https://"+bucketName+"."+endpoint+"/"+objectName; //放在数据库

        }  catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }
}
